/**
 * 通知服务类
 * @author aker
 * @date 2020/4/22
 */

import ChromeCtl from './ChromeCtl'
import Http from './Http'

export default class NotifyClass {
  public static UserAuthorizationErr = "UserAuthorizationErr"
  // 附带按钮的notify
  public static btn(btnTitles:{title:string}[], title:string, msg:string, notifyId:string, iconUrl="./background/Assets/notify-icon.png") {
    const options = {
      type: "basic",
      message: msg,
      iconUrl, title,
      buttons: btnTitles
    }
    chrome.notifications.create(notifyId, options)
  }
  // 关闭notify
  public static async clear(notificationId:string, options?:any) {
    return new Promise((resolve, reject) => {
      chrome.notifications.clear(notificationId, (e:Boolean) => {
        resolve(true)
      })
    })
  }
}

// 当notify按钮被点击时触发
chrome.notifications.onButtonClicked.addListener(async (notifId, btnIdx) => {
  if (notifId === NotifyClass.UserAuthorizationErr) {
    // Warning url host
    await callAsync(ChromeCtl.chromeTabsCreate({url: Http.defaults.baseURL}))
  }
  NotifyClass.clear(notifId)
  printlog(`[Notify.messageId: ${notifId}] btn index:`, btnIdx)
})
